cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
message(STATUS "build:")
Set(EnigineName "HappyStudio")
project(${EnigineName})
set(CMAKE_CXX_STANDARD 17)#C++17
Set(Sln_Dir ${CMAKE_CURRENT_SOURCE_DIR}) #代码首地址
message(STATUS  "CMAKE_CURRENT_SOURCE_DIR:" ${CMAKE_CURRENT_SOURCE_DIR})
#set(NOT_BUILD_DEPS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake_deps/3rdPartyNoProject)
#set(BUILD_3RD_DEPS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake_deps/3rdParty)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../bin)#设置运行目录
message(STATUS "bin dir:" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(DEPS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env)
#请配置  ，改两个地方
if(CMAKE_GENERATOR MATCHES "Visual Studio 17")
    message(STATUS "Compiling with Visual Studio 2022")
	set(DEPS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2022/include)
    set(DEPS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2022/include)
	set(DEPS_LIB ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2022/lib)
	set(LIBCMAKEDIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2022/cmake/igl)#//no use
else()
	if(CMAKE_GENERATOR MATCHES "Visual Studio 16")
	    message(STATUS "compiling with Visual Studio 2019")
		#set(DEPS_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/)
		#set(DEPS_LIB ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2019/lib)
		#set(LIBCMAKEDIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl_env/2019/lib/cmake/igl)#//no use
	else()
	    message(FATAL_ERROR  "build error")
	endif()
endif()


#set(ccache_exe "${DEPS_DIR}/../ccache/ccache.exe")
#https://blog.csdn.net/weixin_30570101/article/details/98075335 vs2019显示编译时间
#find_program(ccache_exe ccache)
if (ccache_exe)
  message ("-- CCache found.")
  file(COPY_FILE
    ${ccache_exe} ${CMAKE_BINARY_DIR}/cl.exe
    ONLY_IF_DIFFERENT)
  add_compile_options("/Z7" )  # 调试信息格式/Zi #    别用"TrackFileAccess=false"
  set(CMAKE_VS_GLOBALS
    "CLToolExe=cl.exe"
    "CLToolPath=${CMAKE_BINARY_DIR}"
    "UseMultiToolTask=true"
    "DebugInformationFormat=OldStyle"
  )
else()
    message("-- CCache not found.")
endif()


if (MSVC)
	set(CMAKE_BUILD_TYPE Release)
	message(STATUS "bin dir2:" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
    #通用设置
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8" )#使用 /MP 选项进行多处理器编译#/std:c++20
	add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)#无论是静态库还是动态库，都可以大幅提升编译速度，比预编译头好用
	#工程在Release调试
	set(CMAKE_CONFIGURATION_TYPES  "Release" CACHE STRING "CONFIGURATION_TYPES" FORCE) #强制Release模式
    option(ENABLE_DEBUG   "ENABLE_DEBUG " ON)#Release是否调试
	option(ENABLE_OPENGL_DEBUG   "ENABLE_OPENGL_DEBUG " ON)#Release是否调试
	if(ENABLE_DEBUG)
		message(STATUS "General Settings release:ENABLE_DEBUG")
		if (NOT ccache_exe)
		    add_compile_options("/Zi" )  # 调试信息格式/Zi
		endif()
		set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Od")#禁用优化速度
		add_link_options(/DEBUG)#链接器生成调试信息
	endif()

	#检测内存泄漏
	#set(CMAKE_CXX_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer")#这个要么加到utf-8里面去
	set(CMAKE_C_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer")
	set(CMAKE_Ｌ_FLAGS "-fsanitize=undefined,address,leak -fno-omit-frame-pointer")
	#代码运行库指定
	#add_compile_options($<$<CONFIG:>:/MD>  $<$<CONFIG:Debug>:/MDd>  $<$<CONFIG:Release>:/MD>)
endif()


set(ENGINE_RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources")
file(TO_NATIVE_PATH "${ENGINE_RESOURCES_DIR}" ENGINE_RESOURCES_DIR_WIN)
message(STATUS "ENGINE_RESOURCES_DIR_WIN fisrt:${ENGINE_RESOURCES_DIR_WIN}")
#other 3rdParty

ADD_SUBDIRECTORY(opengl_env)

ADD_SUBDIRECTORY(Chapter_02_opengl/Prog2_1_background)#Exe
ADD_SUBDIRECTORY(Chapter_02_opengl/Prog2_2_point)#Exe 
ADD_SUBDIRECTORY(Chapter_02_opengl/Prog2_3_errorChecking)
#set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT HappyQt6)#解决方案的默认启动
#else()
#ADD_SUBDIRECTORY(${MainProjectName})#Exe
#set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${MainProjectName})#解决方案的默认启动
#endif()

#折叠ALL_BUILD、ZERO_CHECK
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(PREDEFINED_TARGETS_FOLDER "CustomTargets")




